package com.medical.medical.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.medical.medical.entity.*;
import com.medical.medical.service.*;
import com.medical.medical.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName WldController
 * @Description 请求层
 * @Author WLD123
 * @Date 2022/9/13 22:28
 * @Version 1.0
 */
@Controller
@Slf4j
public class WldController {

    @Resource
    private DepartmentService departmentService;

    @Resource
    private DoctorService doctorService;

    @Resource
    private ReviewService reviewService;

    @Resource
    private DrugService drugService;

    @Resource
    private PatientService patientService;


    @RequestMapping("/getPatient")
    @ResponseBody
    public Object getPatientAll(@RequestParam("id") int id){
        Patient patient = patientService.getPatientAll(id);
        log.info("查询的患者信息:{}",patient);
        return JSONArray.toJSONString(patient);
    }

    /**
     * 修改患者个人信息
     * @param patient
     * @return
     */
    @PostMapping("/modifyPatientById")
    @ResponseBody
    public String modifyPatientById(Patient patient){
        boolean result = patientService.modifyPatientById(patient);
        log.info("result结果:{}",result);
        return JSONArray.toJSONString(result);
    }

    @RequestMapping({"/","/getDepartment"})
    @ResponseBody
    public Object getDepartment(){
        log.info("查询科室开始");
        List<Department> departmentList = departmentService.getDepartmentList();
        /*HashMap<String, Object> map = new HashMap<>();
        map.put("departmentList",departmentList);
        JSONObject json = new JSONObject(map);
        return json;*/
        return JSONArray.toJSONString(departmentList);
    }

    @RequestMapping({"/","/deleteDepartment"})
    @ResponseBody
    public String deleteDepartment(@RequestParam("departmentId") int departmentId){
        log.info("查询科室开始");
        boolean result = departmentService.deleteDepartment(departmentId);
        if (result){
            return "success";
        }
        return "fail";
    }

    /**
     * 根据科室id查询医生信息
     * @param departmentId
     * @return
     */
    @RequestMapping("/getDoctorByDepartmentId")
    @ResponseBody
    public Object getDoctorByDepartmentId(@RequestParam(value = "departmentId", required = false, defaultValue = "0") int departmentId){
        log.info("查询科室里专家/医生信息...");
        log.info("传入ID:{}",departmentId);
        List<Doctor> doctorList = doctorService.getDoctorByDepartmentId(departmentId);
        return JSONArray.toJSONString(doctorList);
    }

    /**
     * 获取所有评论
     * @param map
     * @return
     */
    @RequestMapping({"/","/getAllReview"})
    public Object getAllNoticeAndReview (HashMap<String, Object> map){
        log.info("开始获取所有评论");
        List<Review> allReview = reviewService.getAllReview();
        map.put("noticeList",allReview);
        return "wld/reviewlist";
    }

    /**
     * 添加评论
     * @param review
     * @return
     */
    @PostMapping("/addReview")
    @ResponseBody
    public String addReview(Review review){
        boolean addReviewFlag = reviewService.addReview(review);
        if (addReviewFlag){
            return "success";
        }
        return "fail";
    }

    /**
     * 根据药品名称获取药品
     * @param drugName
     * @return
     */
    @RequestMapping({"/","/getAllDrug"})
    @ResponseBody
    public Object getAllDrug(@RequestParam(value = "drugName",required = false) String drugName){
        List<Drug> drug = drugService.getAllDrug(drugName);
        return JSONArray.toJSONString(drug);
    }

    /**
     * 添加药品
     * @param drug
     * @return
     */
    @RequestMapping("/addDrug")
    @ResponseBody
    public Object addDrug(Drug drug){
        boolean addDrugFlag = drugService.addDrug(drug);
        if (addDrugFlag){
            return "success";
        }
        return "fail";
    }



}
